package com.ctlele.team.space.widget.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.ctlele.team.space.R;
import com.squareup.picasso.Picasso;

public class VodPlayerUI extends RelativeLayout {

    private static final String TAG = "VodPlayerUI";

    private ImageView mBack;

    private TextView mVideoTitle;

    private ImageView mConvertPicture;

    private ImageView mPlayBtn;

    private ImageView mFullScreen;

    private SeekBar mVideoSeek;

    private TextView mCurrTime;

    private TextView mTotalTime;

    private IVodPlayController mController;

    private boolean mPlaying = false;

    private volatile int mTotalMs;

    private volatile boolean mIsFullScreen = false;

    private boolean mIsInit = false;

    public interface IVodPlayController {
        void play();

        void pause();

        void stop();

        void resume();

        void seek(long time);

        void playWithTime(long time);

        void playWithTime(float time);

        void fullScreen();

        void unFullScreen();
    }

    public VodPlayerUI(Context context) {
        super(context);
        initView();
    }

    public VodPlayerUI(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public VodPlayerUI(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public VodPlayerUI(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    void initView() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.view_vod_player_ui, this, true);

        mBack = findViewById(R.id.return_video);
        mVideoTitle = findViewById(R.id.video_title);
        mConvertPicture = findViewById(R.id.covert_img);
        mPlayBtn = findViewById(R.id.play_img);
        mVideoSeek = findViewById(R.id.video_seek);
        mCurrTime = findViewById(R.id.curr_time);
        mTotalTime = findViewById(R.id.total_time);
        mFullScreen = findViewById(R.id.full_screen);

        initData();
    }

    void initData() {
        mPlayBtn.setOnClickListener(view -> {
            if (!mIsInit) {
                clearConvertPicture();
                mIsInit = true;
            }
            if (!mPlaying) {
                mController.resume();
                resumeState();
            } else {
                mController.pause();
                pauseState();
            }
        });

        mVideoSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                mController.pause();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int percent = seekBar.getProgress();
                int seekMs = calPercentValue(percent, mTotalMs);
                mController.playWithTime(convertMillisecondsToSeconds(seekMs));
                mController.playWithTime(seekMs);
                resumeState();
            }
        });

        mFullScreen.setOnClickListener(view -> {
            if (!mIsFullScreen) {
                mController.fullScreen();
                mIsFullScreen = true;
                mFullScreen.setImageResource(R.mipmap.un_full_screen);
            } else {
                mController.unFullScreen();
                mIsFullScreen = false;
                mFullScreen.setImageResource(R.mipmap.full_screen);
            }
        });
    }

    void resumeState() {
        mPlayBtn.setImageResource(R.mipmap.stop_play);
        mPlaying = true;
    }

    void pauseState() {
        mPlayBtn.setImageResource(R.mipmap.play_video);
        mPlaying = false;
    }

    public static int calPercentValue(int percentage, int total) {
        if (total == 0) {
            return 0;
        }
        double value = (double) percentage / 100 * total;
        return (int) Math.round(value);
    }

    public static float convertMillisecondsToSeconds(int milliseconds) {
        return (float) milliseconds / 1000;
    }

    public void end() {
        mController.pause();
        mPlayBtn.setImageResource(R.mipmap.play_video);
        mPlaying = false;
        mVideoSeek.setProgress(100);
        mCurrTime.setText(mTotalTime.getText());
    }

    public void setController(IVodPlayController controller) {
        mController = controller;
    }

    public void setTotalMs(int ms) {
        mTotalMs = ms;
    }

    public void setConvertPicture(String url) {
        if (!TextUtils.isEmpty(url))
            Picasso.with(this.getContext()).load(url).into(mConvertPicture);
    }

    public void clearConvertPicture() {
        if (mConvertPicture == null) return;
        mConvertPicture.setImageDrawable(null);
    }

    public void setVideoTitle(String title) {
        if (!TextUtils.isEmpty(title))
            mVideoTitle.setText(title);
    }

    public void setOnBackPressed(OnClickListener listener) {
        mBack.setOnClickListener(listener);
    }

    public SeekBar getVideoSeekBar() {
        return mVideoSeek;
    }

    public TextView getCurrTime() {
        return mCurrTime;
    }

    public TextView getTotalTime() {
        return mTotalTime;
    }
}
